package com.example.concurrent;

import lombok.extern.slf4j.Slf4j;

public class Test13 {
	public static void main(String[] args) throws InterruptedException {
		TwoPhaseTermination termination = new TwoPhaseTermination();
		termination.start();
		Thread.sleep(3500);
		termination.stop();
	}
}

@Slf4j(topic = "c.twoPhaseTermination")
class TwoPhaseTermination {
	private Thread moniterThread;
	private volatile boolean stop = false;//读屏障 ， 写屏障，防止指令重排序， 可见性， 有序性，原子性

	public void start() {
		moniterThread = new Thread(() -> {
			while (true) {
				Thread current = Thread.currentThread();
				if (stop) {
					log.debug("料理后事");
					break;
				}
				try {
					Thread.sleep(1000);
					log.debug("执行监控记录");
				} catch (InterruptedException e) {
					//current.interrupt();
				}

			}
		}, "moniter");
		moniterThread.start();
	}

	public void stop() {
		stop = true;
		moniterThread.interrupt();
	}
}